/**
 *   类说明
 * 
 *   @creator         xldc	497937995@qq.com  
 *   @create-time     2014年1月20日   下午7:13:51   
 */
package com.huacheng.cms.action.admin;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huacheng.cms.constant.ArticleConstant;
import com.huacheng.cms.constant.FolderConstant;
import com.huacheng.cms.constant.SystemConstant;
import com.huacheng.cms.exception.BaseException;
import com.huacheng.cms.model.Article;
import com.huacheng.cms.model.Folder;
import com.huacheng.cms.model.vo.ArticleVo;
import com.huacheng.cms.model.vo.JsonVo;
import com.huacheng.cms.model.vo.PageVo;

@Controller
@RequestMapping("/admin/article")
public class AdminArticleAction extends AdminBaseAction {

	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String add(@RequestParam(value = "folderId", defaultValue = "1") long folderId, HttpServletResponse response)
			throws BaseException {
		Article article = articleService.addOne(folderId);
		try {
			response.sendRedirect(SystemConstant.BASE_PATH + "/admin/article/update.htm?articleId="
					+ article.getArticleid());
		} catch (IOException e) {
			e.printStackTrace();
			return "system/500";
		}
		return null;
	}

	@RequestMapping(value = "toadd.htm")
	public String toAdd(@RequestParam(value = "folderId", defaultValue = "1") long folderId,
			HttpServletResponse response) {
		return "system/article/add";

	}

	/**
	 * @author 进入某种文章的列表分页的首页
	 * @throws FolderNotFoundException
	 * 
	 */
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String filePage(@RequestParam(value = "p", defaultValue = "1") int pageNum,
			@RequestParam(value = "status", defaultValue = "display") ArticleConstant.Status status,
			@RequestParam(value = "folderId", defaultValue = "0") long folderId, HttpServletRequest request,
			ModelMap modelMap) throws BaseException {

		/* ????? */
		modelMap.put("pathList", "");
		modelMap.put("folderId", folderId);
		PageVo<ArticleVo> pageVo = articleService.getArticlePageByTypeAndStatusPage(folderId, status, pageNum);

		Article article = new Article();
		article.setStatus(ArticleConstant.Status.display.toString());
		int displayCount = articleService.getListCountByObj(article);

		article.setStatus(ArticleConstant.Status.hidden.toString());
		int hiddenCount = articleService.getListCountByObj(article);

		article.setStatus(ArticleConstant.Status.trash.toString());
		int trashCount = articleService.getListCountByObj(article);

		article.setStatus(ArticleConstant.Status.init.toString());
		int initCount = articleService.getListCountByObj(article);
		modelMap.put("displayCount", displayCount);
		modelMap.put("hiddenCount", hiddenCount);
		modelMap.put("trashCount", trashCount);
		modelMap.put("initCount", initCount);
		modelMap.put("status", status);
		modelMap.put("pageVo", pageVo);

		return "system/article/list";
	}

	/**
	 * @author 进入修改文章页面
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(@RequestParam(value = "articleId", defaultValue = "1") long articleId, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Article article = articleService.getOneByKey(articleId);
		modelMap.put("article", article);
		// modelMap.put("folderAll", folderService.getListByObj(new Folder(), 0,
		// 0));

		Folder folder = new Folder();
		folder.setStatus(FolderConstant.Status.display.toString());
		modelMap.put("folderAll", folderService.getListByObj(folder, 0, 0));
		modelMap.put("JSESSIONID", request.getSession().getId());
		return "system/article/update";
	}

	/**
	 * @author 修改文章资料
	 * @param articleId
	 * @param folderId
	 * @param name
	 * @param titile
	 * @param content
	 * @param description
	 * @param status
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<ArticleVo> update(@RequestParam("articleId") long articleId, @RequestParam("folderId") long folderId,
			@RequestParam("name") String name, @RequestParam("tag") String tag,
			@RequestParam("content") String content, @RequestParam("thumbpic") String thumbpic,
			@RequestParam("status") ArticleConstant.Status status, HttpServletRequest request, ModelMap modelMap) {
		JsonVo<ArticleVo> json = new JsonVo<ArticleVo>();
		ArticleVo articleVo = new ArticleVo();
		articleVo.setArticleid(articleId);
		articleVo.setFolderid(folderId);
		articleVo.setName(name);
		articleVo.setContent(content);
		articleVo.setThumbpic(thumbpic);
		articleVo.setStatus(status.toString());

		ArticleVo re_art = articleService.updateOneByObj(articleVo);
		json.setT(re_art);
		json.setResult(true);
		return json;
	}

	/**
	 * @author 彻底删除文件
	 * @throws BaseException
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public JsonVo<String> deleteFile(@RequestParam(value = "articleId") long articleId) throws BaseException {
		JsonVo<String> json = new JsonVo<String>();
		Article article = articleService.getOneByKey(articleId);
		article.setStatus(ArticleConstant.Status.trash.toString());
		articleService.deleteBySelective(article);
		json.setResult(true);
		return json;
	}

	/**
	 * 放进回收站，还原
	 * 
	 * @throws BaseException
	 */
	@ResponseBody
	@RequestMapping(value = "/status/update.json", method = RequestMethod.POST)
	public JsonVo<String> updateModify(@RequestParam(value = "articleId") long articleId,
			@RequestParam(value = "status") ArticleConstant.Status status) throws BaseException {
		JsonVo<String> json = new JsonVo<String>();
		ArticleVo articleVo = articleService.getOneByKey(articleId);
		if (articleVo == null) {
			json.setMsg("找不到此文件");
		} else {
			articleVo.setStatus(status.toString());
			articleService.updateOneByObj(articleVo);
			json.setResult(true);
		}
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/status/delete.json", method = RequestMethod.POST)
	public JsonVo<String> deleteList() throws BaseException {
		JsonVo<String> json = new JsonVo<String>();
		Article article = new Article();
		article.setStatus(ArticleConstant.Status.trash.toString());
		int count = articleService.getListCountByObj(article);
		if (count == 0) {
			json.setResult(true);
		} else {
			int i = articleService.deleteBySelective(article);
			if (i == 1) {
				json.setResult(true);
			} else {
				json.setResult(false);
			}
		}
		return json;
	}
}
